package main

import (
	"encoding/json"
	"net/http"
	//"github.com/golang/protobuf/proto"
	//"errors"
	//"fmt"
	"github.com/playnb/mustang/log"
	"github.com/playnb/mustang/nosql"
	"strconv"
)

type JsonOnlyCount struct {
	Count uint32 `json:"Count"`
}

func GetOnlyCountByKey(key string) []byte {
	count, err := nosql.Redis.Get(key).Int64()
	if err != nil {
		log.Error("Ket:%s  %s", key, err.Error())
		count = 0
	}
	//log.Trace("%s key-> %d", key, count)

	co := &JsonOnlyCount{}
	co.Count = uint32(count)
	bstr, err := json.Marshal(co)
	return (bstr)
}
func SCardOnlyCountByKey(key string) []byte {
	count, err := nosql.Redis.SCard(key).Result()
	if err != nil {
		log.Error("Key:%s  %s", key, err.Error())
		log.Error(err.Error())
		count = 0
	}

	co := &JsonOnlyCount{}
	co.Count = uint32(count)
	bstr, err := json.Marshal(co)
	return (bstr)
}

func handleCurrentOnline(w http.ResponseWriter, r *http.Request) {
	w.Write(GetOnlyCountByKey("ST:CURRENT_ONLINE"))
}

func handleTotalUsers(w http.ResponseWriter, r *http.Request) {
	w.Write(SCardOnlyCountByKey("ST:TOTAL_USERS"))
}

func handleTotalView(w http.ResponseWriter, r *http.Request) {
	w.Write(SCardOnlyCountByKey("ST:TOTAL_VIEW"))
}

func handlePlayGame(w http.ResponseWriter, r *http.Request) {
	w.Write(GetOnlyCountByKey("ST:PLAY_GAME"))
}

func handleViewPage(w http.ResponseWriter, r *http.Request) {
	w.Write(GetOnlyCountByKey("ST:VIEW_PAGE"))
}

func handleStartGame(w http.ResponseWriter, r *http.Request) {
	w.Write(GetOnlyCountByKey("ST:START_GAME"))
}

func handleTaunterOther(w http.ResponseWriter, r *http.Request) {
	w.Write(GetOnlyCountByKey("ST:TAUNTER_OTHER"))
}

func handleShareFirends(w http.ResponseWriter, r *http.Request) {
	w.Write(GetOnlyCountByKey("ST:ShareFirends"))
}
func handleShareTimeline(w http.ResponseWriter, r *http.Request) {
	w.Write(GetOnlyCountByKey("ST:ShareTimeline"))
}

func handleShareFirendUsers(w http.ResponseWriter, r *http.Request) {
	w.Write(SCardOnlyCountByKey("ST:ShareFirendUsers"))
}
func handleShareTimelineUser(w http.ResponseWriter, r *http.Request) {
	w.Write(SCardOnlyCountByKey("ST:ShareTimelineUser"))
}
func handleAvgCostTime(w http.ResponseWriter, r *http.Request) {
	w.Write(GetOnlyCountByKey("ST:AVG:TIMECOST"))
}
func handleMaxTimeCost(w http.ResponseWriter, r *http.Request) {
	w.Write(GetOnlyCountByKey("ST:MaxTimeCost"))
}

func handleBestScore(w http.ResponseWriter, r *http.Request) {
	score := make(map[string]int64)
	jsonStr := "{"
	seperate := ""
	for i := 1000; i > 0; i-- {
		key := "ST:BEST_SCORE_" + strconv.FormatInt(int64(i), 10)
		s := nosql.Redis.SCard(key).Val()
		if s > 0 {
			score[key] = s
			jsonStr = jsonStr + seperate + `"` + key + `":` + strconv.Itoa(int(s))
			seperate = `,`
		}
	}
	jsonStr = jsonStr + "}"
	/*
		b, err := json.Marshal(score)
		if err!=nil{
			log.Error(err.Error())
		}
		log.Debug(jsonStr)
	*/
	w.Write([]byte(jsonStr))
}

func registMonitor(mux *http.ServeMux) {
	mux.HandleFunc("/monitor/CurrentOnline", handleCurrentOnline)
	mux.HandleFunc("/monitor/TotalUsers", handleTotalUsers)
	mux.HandleFunc("/monitor/TotalView", handleTotalView)
	mux.HandleFunc("/monitor/PlayGame", handlePlayGame)
	mux.HandleFunc("/monitor/ViewPage", handleViewPage)
	mux.HandleFunc("/monitor/StartGame", handleStartGame)
	mux.HandleFunc("/monitor/BestScore", handleBestScore)
	mux.HandleFunc("/monitor/TaunterOther", handleTaunterOther)
	mux.HandleFunc("/monitor/ShareFirends", handleShareFirends)
	mux.HandleFunc("/monitor/ShareTimeline", handleShareTimeline)
	mux.HandleFunc("/monitor/ShareFirendUsers", handleShareFirendUsers)
	mux.HandleFunc("/monitor/ShareTimelineUser", handleShareTimelineUser)
	mux.HandleFunc("/monitor/AvgCostTime", handleAvgCostTime)
	mux.HandleFunc("/monitor/MaxTimeCost", handleMaxTimeCost)
}
